﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Report_MonthReport : System.Web.UI.Page
{
    ReportManager dllReportManager = new ReportManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpQuarterDate);
        }
        this.gdvMonthReport.DataSource = new DataTable();
        this.gdvMonthReport.DataBind();
    }

    DataTable getMonthHandShake()
    {
        ListItem li = this.drpMonthDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无月周期")
            {
                DataTable _monthHandShake = dllReportManager.getMonthHandShake(Convert.ToInt32(li.Value));
                return _monthHandShake;
            }
        }
        return new DataTable();
    }


    void bindGridView()
    {

        ListItem li = this.drpMonthDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无月周期")
            {
                DataSet ds = dllReportManager.getMonthReport(Convert.ToInt32(li.Value));
                DataTable _resultTable = ds.Tables[ds.Tables.Count - 1];
                DataTable _monthHandShake = getMonthHandShake();
                for (int i = 0; i < _monthHandShake.Rows.Count; i++)
                {
                    DataRow dr = _resultTable.NewRow();
                    for (int j = 0; j < _resultTable.Columns.Count; j++)
                    {
                        dr[j] = _monthHandShake.Rows[i][j].ToString();
                    }
                    _resultTable.Rows.InsertAt(dr, 0);
                }
                DataTable MonthReport = ds.Tables[ds.Tables.Count - 1];
                DataRow _dataRow = MonthReport.NewRow();
                _dataRow["DepartmentParentName"] = "每条计划占比";
                _dataRow["DepartmentName"] = "每条计划占比";
                MonthReport.Rows.Add(_dataRow);

                //添加未完成项影响总比率列
                DataColumn dc = new DataColumn("部门计划未完成率");
                MonthReport.Columns.Add(dc);
                double TheMonthPlanCount = 0;
                double TheMonthCompleteCount = 0;
                double TheMonthEmphasisPlanCount = 0;
                double PercentUnSuccess = 0;
                for (int i = 0; i < MonthReport.Rows.Count - 1; i++)
                {
                    if (MonthReport.Rows[i][2].ToString().Equals("公司完成合计") || MonthReport.Rows[i][2].ToString().Equals("公司完成百分比") || MonthReport.Rows[i][2].ToString().Equals("计划完成合计") || MonthReport.Rows[i][2].ToString().Equals("计划完成百分比"))
                    {
                    }
                    else
                    {
                        TheMonthPlanCount = Convert.ToInt32(MonthReport.Rows[i]["TheMonthPlanCount"].ToString());
                        TheMonthCompleteCount = Convert.ToInt32(MonthReport.Rows[i]["TheMonthCompleteCount"].ToString());
                        TheMonthEmphasisPlanCount = Convert.ToInt32(MonthReport.Rows[i]["TheEmphasisPlan"].ToString());
                        TheMonthPlanCount = TheMonthPlanCount - TheMonthEmphasisPlanCount;
                        if (TheMonthPlanCount > 0)
                        {
                            PercentUnSuccess = (TheMonthCompleteCount / TheMonthPlanCount) * 100;
                            if (PercentUnSuccess > 0)
                            {
                                MonthReport.Rows[i]["部门计划未完成率"] = PercentUnSuccess.ToString("0.00");
                            }
                            else
                            {
                                MonthReport.Rows[i]["部门计划未完成率"] = "";
                            }
                        }
                    }
                }

                double SumPercent = 0;
                //for (int i = 0; i < MonthReport.Rows.Count; i++)
                //{
                //    if (MonthReport.Rows[i]["未完成项影响总比率"].ToString().Length == 0 || MonthReport.Rows[i][2].ToString().Equals("计划完成合计") || MonthReport.Rows[i][2].ToString().Equals("计划完成百分比"))
                //    {
                //    }
                //    else
                //    {
                //        SumPercent += Convert.ToDouble(MonthReport.Rows[i]["未完成项影响总比率"].ToString());
                //    }
                //    if (MonthReport.Rows[i][2].ToString().Equals("计划完成百分比") && SumPercent > 0)
                //    {
                //        MonthReport.Rows[i]["未完成项影响总比率"] = SumPercent.ToString();
                //        SumPercent = 0;
                //    }                   
                //}

                this.gdvMonthReport.DataSource = MonthReport;
                this.gdvMonthReport.DataBind();
                MergeRow(this.gdvMonthReport, 0);
                Process(this.gdvMonthReport);

                object[] _value = new object[1];
                _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
                int _preMonthDate = dllMonthDateManager.getPreMonthDate(_value);
                #region 添加计划链接
                for (int i = 1; i < this.gdvMonthReport.Rows.Count - 1; i++)
                {
                    if (this.gdvMonthReport.Rows[i].Cells[1].Text != "公司完成百分比" && this.gdvMonthReport.Rows[i].Cells[2].Text != "计划完成百分比" && this.gdvMonthReport.Rows[i].Cells[2].Text != "&nbsp;")
                    {
                        if (this.gdvMonthReport.Rows[i].Cells[1].Text == "计划完成合计" || this.gdvMonthReport.Rows[i].Cells[1].Text == "公司完成合计")
                        {
                        }
                        else
                        {
                            //上月总数                    
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[2].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += _preMonthDate + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=1' target='_blank'>";
                                this.gdvMonthReport.Rows[i].Cells[2].Text = _url + this.gdvMonthReport.Rows[i].Cells[2].Text + "</a>";
                            }
                            //上月完成总数
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[3].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += _preMonthDate + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=4' target='_blank'>";
                                this.gdvMonthReport.Rows[i].Cells[3].Text = _url + this.gdvMonthReport.Rows[i].Cells[3].Text + "</a>";
                            }
                            //本月总数
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[5].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += Convert.ToInt32(this.drpMonthDate.SelectedValue) + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=1' target='_blank'>";
                                this.gdvMonthReport.Rows[i].Cells[5].Text = _url + this.gdvMonthReport.Rows[i].Cells[5].Text + "</a>";
                            }
                            //本月完成总数
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[6].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += Convert.ToInt32(this.drpMonthDate.SelectedValue) + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=4' target='_blank'>";
                               this.gdvMonthReport.Rows[i].Cells[6].Text = _url + this.gdvMonthReport.Rows[i].Cells[6].Text + "</a>";
                            }
                            //本月顺利总数
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[7].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += Convert.ToInt32(this.drpMonthDate.SelectedValue) + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=5' target='_blank'>";
                                this.gdvMonthReport.Rows[i].Cells[7].Text = _url + this.gdvMonthReport.Rows[i].Cells[7].Text + "</a>";
                            }
                            //本月障碍总数
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[8].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += Convert.ToInt32(this.drpMonthDate.SelectedValue) + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=6' target='_blank'>";
                                this.gdvMonthReport.Rows[i].Cells[8].Text = _url + this.gdvMonthReport.Rows[i].Cells[8].Text + "</a>";
                            }
                            //本月失控总数
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[9].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += Convert.ToInt32(this.drpMonthDate.SelectedValue) + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=7' target='_blank'>";
                                this.gdvMonthReport.Rows[i].Cells[9].Text = _url + this.gdvMonthReport.Rows[i].Cells[9].Text + "</a>";
                            }
                            //本月调整总数
                            if (Convert.ToInt32(this.gdvMonthReport.Rows[i].Cells[10].Text) > 0)
                            {
                                string _department = Server.UrlEncode(this.gdvMonthReport.Rows[i].Cells[1].Text);
                                string _url = "<a href='ViewPlan.aspx?PlanType=2&MonthDate=";
                                _url += Convert.ToInt32(this.drpMonthDate.SelectedValue) + "&_department=";
                                _url += _department;
                                _url += "&PlanSendState=8' target='_blank'>";
                                this.gdvMonthReport.Rows[i].Cells[10].Text = _url + this.gdvMonthReport.Rows[i].Cells[10].Text + "</a>";
                            }
                        }

                    }
                }
                #endregion
                #region 添加握手链接
                if (this.gdvMonthReport.Rows.Count >= 1)
                {
                    if (this.gdvMonthReport.Rows[0].Cells[0].Text == "握手关系")
                    {
                        if (Convert.ToInt32(this.gdvMonthReport.Rows[0].Cells[2].Text) > 0)
                        {
                            string _url = "<a href='ViewHandShake.aspx?Type=1&MonthDate=";
                            _url += _preMonthDate;
                            _url += "&PlanSendState=1' target='_blank'>";
                            this.gdvMonthReport.Rows[0].Cells[2].Text = _url + this.gdvMonthReport.Rows[0].Cells[2].Text + "</a>";
                        }
                        if (Convert.ToInt32(this.gdvMonthReport.Rows[0].Cells[3].Text) > 0)
                        {
                            string _url = "<a href='ViewHandShake.aspx?Type=1&MonthDate=";
                            _url += _preMonthDate;
                            _url += "&PlanSendState=4' target='_blank'>";
                            this.gdvMonthReport.Rows[0].Cells[3].Text = _url + this.gdvMonthReport.Rows[0].Cells[3].Text + "</a>";
                        }
                        if (Convert.ToInt32(this.gdvMonthReport.Rows[0].Cells[4].Text) > 0)
                        {
                            string _url = "<a href='ViewHandShake.aspx?Type=1&MonthDate=";
                            _url += Convert.ToInt32(this.drpMonthDate.SelectedValue);
                            _url += "&PlanSendState=1' target='_blank'>";
                            this.gdvMonthReport.Rows[0].Cells[4].Text = _url + this.gdvMonthReport.Rows[0].Cells[4].Text + "</a>";
                        }
                        if (Convert.ToInt32(this.gdvMonthReport.Rows[0].Cells[5].Text) > 0)
                        {
                            string _url = "<a href='ViewHandShake.aspx?Type=1&MonthDate=";
                            _url += Convert.ToInt32(this.drpMonthDate.SelectedValue);
                            _url += "&PlanSendState=4' target='_blank'>";
                            this.gdvMonthReport.Rows[0].Cells[5].Text = _url + this.gdvMonthReport.Rows[0].Cells[5].Text + "</a>";
                        }
                        if (Convert.ToInt32(this.gdvMonthReport.Rows[0].Cells[6].Text) > 0)
                        {
                            string _url = "<a href='ViewHandShake.aspx?Type=1&MonthDate=";
                            _url += Convert.ToInt32(this.drpMonthDate.SelectedValue);
                            _url += "&PlanSendState=5' target='_blank'>";
                            this.gdvMonthReport.Rows[0].Cells[6].Text = _url + this.gdvMonthReport.Rows[0].Cells[6].Text + "</a>";
                        }
                        if (Convert.ToInt32(this.gdvMonthReport.Rows[0].Cells[7].Text) > 0)
                        {
                            string _url = "<a href='ViewHandShake.aspx?Type=1&MonthDate=";
                            _url += Convert.ToInt32(this.drpMonthDate.SelectedValue);
                            _url += "&PlanSendState=6' target='_blank'>";
                            this.gdvMonthReport.Rows[0].Cells[7].Text = _url + this.gdvMonthReport.Rows[0].Cells[7].Text + "</a>";
                        }
                        if (Convert.ToInt32(this.gdvMonthReport.Rows[0].Cells[8].Text) > 0)
                        {
                            string _url = "<a href='ViewHandShake.aspx?Type=1&MonthDate=";
                            _url += Convert.ToInt32(this.drpMonthDate.SelectedValue);
                            _url += "&PlanSendState=7' target='_blank'>";
                            this.gdvMonthReport.Rows[0].Cells[8].Text = _url + this.gdvMonthReport.Rows[0].Cells[8].Text + "</a>";
                        }
                    }
                }
                #endregion
            }
        }
    }

    /// <summary>
    /// 生成报表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnGetReport_Click(object sender, EventArgs e)
    {
        bindGridView();
        if (this.gdvMonthReport.Rows.Count > 0)
        {

            this.gdvMonthReport.Rows[0].Cells[0].ColumnSpan = 2;
            this.gdvMonthReport.Rows[0].Cells[1].Visible = false;
            this.gdvMonthReport.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 2].Cells[0].ColumnSpan = 2;
            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 2].Cells[1].Visible = false;
            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 2].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 1].Cells[0].ColumnSpan = 2;
            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 1].Cells[1].Visible = false;
            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 1].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 3].Cells[0].ColumnSpan = 2;
            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 3].Cells[1].Visible = false;
            this.gdvMonthReport.Rows[this.gdvMonthReport.Rows.Count - 3].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvMonthReport.Columns[0].HeaderStyle.Width = 90;
            this.gdvMonthReport.Columns[0].ItemStyle.Width = 90;
            this.gdvMonthReport.Columns[1].HeaderStyle.Width = 90;
            this.gdvMonthReport.Columns[1].ItemStyle.Width = 90;


        }
    }

    void Process(GridView _gridView)
    {
        _gridView.Columns[3].HeaderStyle.BackColor = System.Drawing.Color.FromName("#00C0C0");
        _gridView.Columns[6].HeaderStyle.BackColor = System.Drawing.Color.FromName("#00C0C0");
        _gridView.Columns[7].HeaderStyle.BackColor = System.Drawing.Color.FromName("#00C000");
        _gridView.Columns[8].HeaderStyle.BackColor = System.Drawing.Color.Yellow;
        _gridView.Columns[9].HeaderStyle.BackColor = System.Drawing.Color.Red;
        _gridView.Columns[3].ItemStyle.BackColor = System.Drawing.Color.FromName("#00C0C0");
        _gridView.Columns[6].ItemStyle.BackColor = System.Drawing.Color.FromName("#00C0C0");
        _gridView.Columns[7].ItemStyle.BackColor = System.Drawing.Color.FromName("#00C000");
        _gridView.Columns[8].ItemStyle.BackColor = System.Drawing.Color.Yellow;
        _gridView.Columns[9].ItemStyle.BackColor = System.Drawing.Color.Red;
        _gridView.Columns[4].ItemStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        _gridView.Columns[4].HeaderStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        _gridView.Columns[10].ItemStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        _gridView.Columns[10].HeaderStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        System.Drawing.Color[] _color = new System.Drawing.Color[5];
        _color[0] = System.Drawing.Color.Gray;
        _color[1] = System.Drawing.Color.Cornsilk;
        _color[2] = System.Drawing.Color.Brown;
        _color[3] = System.Drawing.Color.Orange;
        _color[4] = System.Drawing.Color.Purple;
        _gridView.Rows[1].Cells[0].BackColor = _color[0];
        for (int i = 0; i < _gridView.Rows.Count; i++)
        {
            if (_gridView.Rows[i].Cells[1].Text == "计划完成百分比" || _gridView.Rows[i].Cells[1].Text == "公司完成百分比")
            {
                double percent = Convert.ToDouble(_gridView.Rows[i].Cells[3].Text);

                _gridView.Rows[i].Cells[3].Text = string.Format("{0:P}", percent);
                percent = Convert.ToDouble(_gridView.Rows[i].Cells[6].Text);

                _gridView.Rows[i].Cells[6].Text = string.Format("{0:P}", percent);
            }
        }
        int _index = 0;
        for (int i = 1; i < _gridView.Rows.Count; i++)
        {

            if (_gridView.Rows[i].Cells[1].Text == "计划完成合计")
            {
                _gridView.Rows[i].BackColor = _color[_index];
                _gridView.Rows[i].Cells[3].BackColor = _color[_index];
                _gridView.Rows[i].Cells[4].BackColor = _color[_index];
                _gridView.Rows[i].Cells[5].BackColor = _color[_index];
                _gridView.Rows[i].Cells[6].BackColor = _color[_index];
                _gridView.Rows[i].Cells[7].BackColor = _color[_index];
                _gridView.Rows[i].Cells[8].BackColor = _color[_index];
                _gridView.Rows[i].Cells[9].BackColor = _color[_index];
                _gridView.Rows[i].Cells[10].BackColor = _color[_index];
            }
            if (_gridView.Rows[i].Cells[1].Text == "计划完成百分比")
            {
                _gridView.Rows[i].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[3].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[4].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[5].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[6].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[7].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[8].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[9].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[10].BackColor = _gridView.Rows[i - 1].BackColor;
                _index++;
                if (_index > _color.Length - 1)
                {
                    _index = 0;
                }
                _gridView.Rows[i + 1].Cells[0].BackColor = _color[_index];
            }
        }
        _gridView.Rows[_gridView.Rows.Count - 3].Cells[0].BackColor = new System.Drawing.Color();
        _gridView.Rows[_gridView.Rows.Count - 3].Cells[3].BackColor = System.Drawing.Color.FromName("#ffffff");

        _gridView.Rows[_gridView.Rows.Count - 1].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[3].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[4].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[5].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[6].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[7].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[8].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[9].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[10].BackColor = System.Drawing.Color.Red;

        _gridView.Rows[_gridView.Rows.Count - 2].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[3].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[4].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[5].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[6].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[7].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[8].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[9].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[10].BackColor = System.Drawing.Color.Red;

        #region 处理计划占比
        double _preCount = Convert.ToInt32(_gridView.Rows[_gridView.Rows.Count - 3].Cells[2].Text);
        double _Count = Convert.ToInt32(_gridView.Rows[_gridView.Rows.Count - 3].Cells[5].Text);
        double _prePlan = 0.0;
        double _Plan = 0.0;
        if (_preCount != 0)
        {
            _prePlan = 1 / _preCount;
        }

        _prePlan = _prePlan * 100;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[2].Text = _prePlan.ToString("0.00") + "%";
        if (_Count != 0)
        {
            _Plan = 1 / _Count;
        }
        _Plan = _Plan * 100;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[5].Text = _Plan.ToString("0.00") + "%";
        #endregion
    }

    /// <summary>
    /// 合并同一列相同值得行
    /// </summary>
    /// <param name="_gridView"></param>
    /// <param name="_cellNum"></param>
    void MergeRow(GridView _gridView, int _cellNum)
    {
        int i = 0, rowSpanNum = 1;
        //遍历GridView
        while (i < _gridView.Rows.Count - 1)
        {
            //获得当前行
            GridViewRow gvr = _gridView.Rows[i];
            for (++i; i < _gridView.Rows.Count; i++)
            {
                //获得下一行
                GridViewRow gvrNext = _gridView.Rows[i];
                if (gvr.Cells[_cellNum].Text == gvrNext.Cells[_cellNum].Text)
                {
                    gvrNext.Cells[_cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[_cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                }
                //遍历完毕进行合并
                if (i == _gridView.Rows.Count - 1)
                {
                    gvr.Cells[_cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }




    /// <summary>
    /// 合并列头
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvMonthReport_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            rowHeader.BackColor = System.Drawing.Color.White;
            rowHeader.Font.Bold = true;
            TableCellCollection cells = e.Row.Cells;
            int i = 0;
            int colSpanNum = 1;
            TableCell headerCell = cells[i];
            for (++i; i < cells.Count; i++)
            {
                TableCell headerNextCell = cells[i];
                if (headerCell.Text == headerNextCell.Text)
                {
                    headerNextCell.Visible = false;
                    colSpanNum++;
                }
                else
                {
                    headerCell.ColumnSpan = colSpanNum;
                    colSpanNum = 1;
                    break;
                }
                //遍历完毕进行合并
                if (i == cells.Count - 1)
                {
                    headerCell.ColumnSpan = colSpanNum;
                }
            }


            this.gdvMonthReport.Controls[0].Controls.AddAt(0, rowHeader);
            TableCell headerCellMonth = new TableCell();
            headerCellMonth.Text = "";
            headerCellMonth.ColumnSpan = 2;
            rowHeader.Cells.Add(headerCellMonth);

            headerCellMonth = new TableCell();
            headerCellMonth.Text = "上月";
            headerCellMonth.ColumnSpan = 3;
            headerCellMonth.HorizontalAlign = HorizontalAlign.Center;
            rowHeader.Cells.Add(headerCellMonth);

            headerCellMonth = new TableCell();
            headerCellMonth.Text = "本月";
            headerCellMonth.ColumnSpan = cells.Count - 1;
            headerCellMonth.HorizontalAlign = HorizontalAlign.Center;

            rowHeader.Cells.Add(headerCellMonth);
            rowHeader.Visible = true;
            this.gdvMonthReport.Controls[0].Controls.AddAt(0, rowHeader);
        }

    }


    /// <summary>
    /// 月完成图表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnChart_Click(object sender, EventArgs e)
    {
        ListItem li = this.drpMonthDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无月周期")
            {
                string _url = "MonthReportCompleteRate.aspx?MonthDate=" + Convert.ToInt32(li.Value);
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=740,width=1015,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            }
        }

    }


    /// <summary>
    /// 部门完成图表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDepartmentChart_Click(object sender, EventArgs e)
    {
        ListItem li = this.drpMonthDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无月周期")
            {
                string _url = "MonthDepartmentCompleteRate.aspx?MonthDate=" + Convert.ToInt32(li.Value);
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=740,width=1015,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            }
        }
    }




    /// <summary>
    /// 绑定DropDownList
    /// </summary>   
    void bindDropDownList(DropDownList _drp)
    {

        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
        }
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前月周期
                //根据系统当前日期查找到对应的月周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                    bindDropDownList(this.drpMonthDate);
                }
                #endregion

            }
            else
            {
                ListItem li = new ListItem("无月周期");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(li);
                bindDropDownList(this.drpMonthDate);
            }

        }
        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                DataTable _monthtime = dllMonthDateManager.getMonthDate();
                if (_monthtime.Rows.Count == 1)
                {
                    this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                    this.drpMonthDate.DataSource = _monthtime;
                    this.drpMonthDate.DataTextField = "MonthName";
                    this.drpMonthDate.DataValueField = "Month_Id";

                }
                #endregion

            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);

            }
        }

    }
    /// <summary>
    /// 年份索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        this.gdvMonthReport.DataSource = new DataTable();
        this.gdvMonthReport.DataBind();
    }
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);
        this.gdvMonthReport.DataSource = new DataTable();
        this.gdvMonthReport.DataBind();
    }
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {

    }



    /// <summary>
    /// 月周期统计投影按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        string _url = "MonthReportDisplay.aspx";
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=715,width=1015,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }


    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GridView[] _gridView = new GridView[1];
        bindGridView();
        MergeRow(this.gdvMonthReport, 0);
        _gridView[0] = this.gdvMonthReport;
        Utility.Comm.MonthReportToExcel(GridViewToDataTable(_gridView)[0], this.drpQuarterDate.SelectedItem.Text + this.drpMonthDate.SelectedItem.Text);
    }



    /// <summary>
    /// GridView转DataTable
    /// </summary>
    DataTable[] GridViewToDataTable(GridView[] _sourceGridView)
    {
        DataTable[] _targetDataTable = new DataTable[_sourceGridView.Length];
        for (int i = 0; i < _targetDataTable.Length; i++)
        {
            _targetDataTable[i] = new DataTable();
            DataColumn dc = new DataColumn();
            GridViewRow gr = _sourceGridView[i].HeaderRow;
            //根据源GridView循环创建目标DataTable列
            for (int j = 0; j < gr.Cells.Count; j++)
            {
                //if (gr.Cells[j].Visible == true)
                //{
                dc = new DataColumn(gr.Cells[j].Text);
                if (dc.ColumnName == "计划总数" || dc.ColumnName == "完成" || dc.ColumnName == "部门")
                {
                    Guid gd = Guid.NewGuid();
                    dc.ColumnName = dc.ColumnName + gd.ToString();
                }
                _targetDataTable[i].Columns.Add(dc);
                //}
            }
            //根据源GridView循环创建目标DataTable行
            for (int j = 0; j < _sourceGridView[i].Rows.Count; j++)
            {
                DataRow dr = _targetDataTable[i].NewRow();
                for (int k = 0; k < gr.Cells.Count; k++)
                {

                    if (k < _targetDataTable[i].Columns.Count)
                    {

                        dr[k] = _sourceGridView[i].Rows[j].Cells[k].Text;
                    }
                }
                _targetDataTable[i].Rows.Add(dr);
            }
        }
        return _targetDataTable;
    }
}
